# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： applets_api_action.py
# @Time：2023/8/9 10:00
# @Author：lizhixiong
import json

import allure

from common.commin.commin import read_ini
from common.log.log import log
from config.conf import BASE_DIR
from po.jinhua.applets_po_api.applets_api import AppletsApi
from po.jinhua.ht.api.ht_api import ht_api_send
from po.public_methods import yg_applets_wx_api_login

# 环境地址
url_yg = read_ini(BASE_DIR + '/config_yg.ini')['host']['url']

"""
主要用于场景化的流程脚本（多个步骤组合）
"""


@allure.step('直接生成一个订单')
def generate_an_order():
    """获取token"""
    applets_token = yg_applets_wx_api_login()

    # 商品信息（商品id，规格id，库存，销量，赠送积分，售价）
    sp_list = AppletsApi(url_yg).get_yaml_sp_info(applets_token)
    product_num = 10

    # 创建订单并支付成功
    order_id = AppletsApi(url_yg).api_ordering_yg(applets_token, 'buyNow', sp_list[1], sp_list[0], product_num, 0)
    return order_id


@allure.step('下单完整流程')
def order_action(applets_token, ht_token, productNum):
    """
    下单完整流程
    Args:
        applets_token: 小程序token
        ht_token:管理台token
        productId:商品id
        attrValueId:规格id
        productNum:购买数
        isVip:是否vip

    Returns:

    """
    try:
        """
        ================测试环境才执行下单接口测试================
        """
        if 'test' in url_yg:
            # 商品信息（商品id，规格id，库存，销量，赠送积分，售价）
            sp_list = AppletsApi(url_yg).get_yaml_sp_info(applets_token)

            # 创建订单并支付成功
            order_id = AppletsApi(url_yg).api_ordering_yg(applets_token, 'buyNow', sp_list[1], sp_list[0], productNum,
                                                          0)
            ht_api_send(ht_token, order_id, productNum)
            AppletsApi(url_yg).api_applets_confirm_receipt(applets_token, order_id)
            AppletsApi(url_yg).api_applets_order_evaluate(applets_token, order_id)
            return order_id
        else:
            log.info('############# 生产环境暂不能下单 #############')
    except Exception as e:
        log.error(f'用例执行过程中出现异常，异常为:{e}')
        raise e


if __name__ == '__main__':
    print(generate_an_order())
